package com.example.jdai.jdai.ui;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.jdai.jdai.R;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by hellopyj on 2017/1/11.
 */

public class MyYzm extends LinearLayout {
    MyYzmClick myYzmClick;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.ll_tip)
    LinearLayout llTip;
    TimeCount time;
    @BindString(R.string.text_phone_getcode)
    String getCode;

    public MyYzm(Context context) {
        super(context);
        init(context, null);
    }

    public MyYzm(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyYzm(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public MyYzm(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.my_yzm, this);
        ButterKnife.bind(this);
        time = new TimeCount(60000, 1000);
        llTip.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(myYzmClick!=null) {
                    boolean mark=myYzmClick.click();
                    if (mark)
                        time.start();
                }
            }
        });
    }

    void setMyYzmClick(MyYzmClick myYzmClick) {
        this.myYzmClick = myYzmClick;
    }

    public interface MyYzmClick {
        boolean click();
    }
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //tvTip.setBackgroundColor(Color.parseColor("#B6B6D8"));
            llTip.setClickable(false);
            tvTip.setText("("+millisUntilFinished / 1000 +")s后再获取");
        }

        @Override
        public void onFinish() {
            tvTip.setText(getCode);
            llTip.setClickable(true);
            //btnGetcode.setBackgroundColor(Color.parseColor("#4EB84A"));

        }
    }
    public void setStop(){
        time.cancel();
        tvTip.setText(getCode);
        llTip.setClickable(true);
    }
}
